package com.sdata.customize.controller;

import com.sdata.core.text.StringUtils;
import com.sdata.customize.pojo.ApplyParams;
import com.sdata.customize.service.AnJiFlowApplyCustomizeService;
import com.sdata.foundation.core.util.ResultInfo;
import com.sdata.foundation.core.util.SDException;
import com.sdata.meta.commons.MetadataConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Slf4j
@Controller
@Path("/ext/anji")
public class AnJiFlowApplyCustomizeController {
    @Autowired
    private AnJiFlowApplyCustomizeService anJiFlowApplyCustomizeService;

    @POST
    @Path("/flow/apply")
    @Produces(MediaType.APPLICATION_JSON)
    public String apply(@RequestBody ApplyParams applyParam){

        if (applyParam == null || StringUtils.isEmpty(applyParam.getDataId()) ||
                StringUtils.isEmpty(applyParam.getFormId()) || StringUtils.isEmpty(applyParam.getUserId())) {
            return new ResultInfo(MetadataConstants.RespondStatus.ERROR, "发起失败", "参数不全!").toString();
        }

        try {
            anJiFlowApplyCustomizeService.apply(applyParam);
            return new ResultInfo(MetadataConstants.RespondStatus.SUCESS, "发起成功！", null).toString();
        } catch (SDException e1) {
            log.error("查询失败：", e1);
            return new ResultInfo(MetadataConstants.RespondStatus.ERROR, e1.getMessage(), null).toString();
        } catch (Exception e) {
            log.error("查询失败:", e);
            return new ResultInfo(MetadataConstants.RespondStatus.ERROR, e.getMessage(), null).toString();
        }
    }
}
